package com.simple.datax.flow.preprocessor.sendprotocol;

import java.util.Date;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.flow.constants.MessageConstants;
import com.simple.datax.flow.constants.ProcessResultConstants;
import com.simple.datax.flow.convertor.Convertor;
import com.simple.datax.flow.dao.ibatis.model.FlowTransmission;
import com.simple.datax.flow.dao.ibatis.model.TaskDec;
import com.simple.datax.flow.message.EcFlowMessage;
import com.simple.datax.flow.service.PreProcessorManager;
import com.simple.datax.flow.service.TaskManager;
import com.simple.datax.flow.service.TaskMessageManager;

/** 
 * @Title: TcsBPNoTransmissionProtocol.java 
 * @Package com.simple.datax.flow.preprocessor.sendprotocol 
 * @Description: TODO(向tcs发送报文前报文流程号和企业代码转换协议实现) 
 * @author tonney 
 * @date 2013-1-25 上午10:26:46 
 * @version V1.0 
 */
public class TcsBPNoTransmissionProtocol implements SendProtocolIF{
	public Logger log = LoggerFactory.getLogger(this.getClass());
	private PreProcessorManager preProcessorManager;
	
	private TaskMessageManager taskMessageManager;

	public void setTaskMessageManager(TaskMessageManager taskMessageManager) {
		this.taskMessageManager = taskMessageManager;
	}

	public void setPreProcessorManager(PreProcessorManager preProcessorManager) {
		this.preProcessorManager = preProcessorManager;
	}
	public int process(EcFlowMessage ecMessage) {
		int result = ProcessResultConstants.STATUS_SUCCESS;
		try{
			String ecode = "";
			String enode = "";
			if(ecMessage.getOrgSendId()!=null){
				String[] enodeArray = ecMessage.getOrgSendAddr().split("@");
				if(enodeArray.length>=2){
					ecode = enodeArray[0];
					enode = enodeArray[1];
				}
			}
			ecMessage.getFlowMessageHead().setSenderId(ecMessage.getFlowMessageBody().getUserId());
			FlowTransmission transmission = new FlowTransmission();
			transmission.seteCode(ecode);
			transmission.seteNode(enode);
			transmission = preProcessorManager.getTransmission(transmission,ecMessage.getFlowMessageBody().getFlowId());
			if(transmission!=null){
				ecMessage.getFlowMessageHead().setSenderId(transmission.getTransSenderId());
				ecMessage.getFlowMessageHead().setSenderAddress(transmission.getTransSenderAddr());
				ecMessage.getFlowMessageBody().setFlowId(transmission.getTransBpNo());
				ecMessage.getFlowMessageBody().setUserId(transmission.getTransSenderId());
				ecMessage.getFlowMessageBody().getActivityIdList().clear();
				ecMessage.getFlowMessageBody().getActivityIdList().add("a01");
				ecMessage.getFlowMessageHead().setReceiverAddress(ecMessage.getFlowMessageHead().getReceiverId()+"@"+transmission.getTcsNode());
			}else{
				log.warn("TcsBPNoTransmissionProtocol::process transmission is null!");
			}
			/****************保存原始报文BEGIN*********************/
			TaskDec taskDec = new TaskDec();
			taskDec.setMessageSeq(ecMessage.getFlowMessageHead().getMessageId());
			taskDec.setActivityNo(ecMessage.getFlowMessageBody().getActivityIdList().get(0));
			taskDec.setTaskId(ecMessage.getFlowMessageBody().getTaskId());
			taskDec.setInputDbTime(new Date());
			taskDec.setDecMsg(ecMessage.getOrgXmlCode().getBytes(MessageConstants.ENCODING_UTF));
			taskMessageManager.saveTaskDec(taskDec);
			/****************保存原始报文END************************/
		}catch(Exception e){
			result = ProcessResultConstants.STATUS_SEND_PROCESS_EXCEPTION;
			log.error("TcsBPNoTransmissionProtocol::process error=>"+e.toString());
		}
		return result;
	}
}
